package com.itheima.train.manager.order.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @program: itheima-train-manager-parent
 * @description: ex同步订单数据的时候, 消息发送方
 * @author: zhanghz001
 * @create: 2021-07-06 14:24
 **/
// @Service
public class ZhzRabbitmqProducer implements RabbitTemplate.ConfirmCallback {
    // 创建日志对象
    private Logger log = LoggerFactory.getLogger(ZhzRabbitmqProducer.class);
    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    // 发送 订单数据到mq中
    public void sendMessage(String jsonData, String routingKey) {
        //创建correlationData对象
        CorrelationData correlationData = new CorrelationData(jsonData);
        
        //设置消息回调函数
        rabbitTemplate.setConfirmCallback(this);
        
        //消息发送
        rabbitTemplate.convertAndSend("train_manager_ex", routingKey, jsonData, correlationData);
    }
    
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {
            log.info("[发送给mq队列成功]---->{}", correlationData.getId());
        } else {
            log.error("[发送给mq队列失败]---->{}", correlationData.getId());
        }
    }
}
